//
//  MYY_registerViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/27.
//  Copyright © 2017年 hbjApple. All rights reserved.
//注册

#import "MYY_registerViewController.h"
#import "MYY_registerView.h"
#import "SexSelectView.h"

#import "KGModal.h"

#import "MYY_User.h"
#import "NJXTLoginHelper.h"


@interface MYY_registerViewController ()

@property(nonatomic, strong) MYY_registerView *MYY_registerVi;
@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;
@property(nonatomic, strong) SexSelectView *sexView;
@property(nonatomic, strong) NSString *sex;
@property(nonatomic, strong) NSString *InvitationCode;
@property(nonatomic, strong) NSString *account;
@property(nonatomic, strong) NSString *code;
@property(nonatomic, strong) NSString *password;

@end

@implementation MYY_registerViewController

-(void)leftButt
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(WIDTH-50, 44, 30, 30);
    [btn setImage:[UIImage imageNamed:@"guanbi-2"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    //  [self.navigationItem setRightBarButtonItem:cancelItem animated:NO];
    
}
-(void)exit
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _sex = @"1";
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.MYY_registerVi];
    [self setupData];
}
- (void)setupData
{
    WS(weakSelf);
    NSArray *userList = [[NJXTLoginHelper helper] listAllUsers];
    [self.MYY_registerVi setUserList:userList loginBlock:^(NSString *InvitationCode, NSString *account, NSString *code, NSString *password) {
        weakSelf.InvitationCode = InvitationCode;
        weakSelf.account = account;
        weakSelf.code = code;
        weakSelf.password = password;
        [weakSelf.sexView.sureSexBtn addTarget:weakSelf action:@selector(sureSex) forControlEvents:UIControlEventTouchUpInside];
        [weakSelf.sexView.maleBtn addTarget:weakSelf action:@selector(maleAct:) forControlEvents:UIControlEventTouchUpInside];
        [weakSelf.sexView.femaleBtn addTarget:weakSelf action:@selector(femaleAct:) forControlEvents:UIControlEventTouchUpInside];
        [KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeNone;
        [KGModal sharedInstance].tapOutsideToDismiss = NO;
        [KGModal sharedInstance].modalBackgroundColor = [UIColor whiteColor];
        [[KGModal sharedInstance] showWithContentView:weakSelf.sexView];
        
    }];
    
//    self.MYY_registerVi.XVerificationB = ^(CodeButton *btn){
//        GRLog(@"获取验证码");
//       [weakSelf.MYY_MessageViewMode identify:@"" success:^(NSString *dataStr) {
//            [JXTHUDHelper showHUDText:@"发送成功" type:@"success" inView:nil];
//       } failure:^(NSString *errorCode) {
//            [JXTHUDHelper showHUDText:@"请稍后重试！" type:@"error" inView:nil];
//       } netDown:^{
//           
//       }];
//       
//         [btn startCountdown];
//    };
    [self leftButt];
}

#pragma mark - property
- (MYY_registerView *)MYY_registerVi
{
    if (_MYY_registerVi == nil) {
        _MYY_registerVi = [[MYY_registerView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _MYY_registerVi.backgroundColor =MYYColorFromRGB(0x8fdcd4);
    }
    return _MYY_registerVi;
}
- (SexSelectView *)sexView{
    if (_sexView == nil) {
        _sexView = [SexSelectView loadNibWithSexSelectView];

    }
    return _sexView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - PrivateMethod
- (void)sureSex{
    [[KGModal sharedInstance] hideAnimated:YES];
    [self.MYY_registerVi updateEnableState:NO hasLogin:YES];
    [self.MYXHttpManag registeraccount:self.account code:self.code Invitation:self.InvitationCode password:self.password sex:self.sex  success:^(MYY_User *user) {
        [user save];
        if (self.registerBlock) {
            self.registerBlock(self.account, self.password);
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    }failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
        [JXTHUDHelper showHUDText:@"网络出错" type:@"error" inView:nil];
    }];
}
- (void)maleAct:(UIButton *)btn{
    
    if (btn.isSelected) {
        return;
    }
    //btn.selected = !btn.selected;
    self.sex = @"1";
    _sexView.maleBtn.selected = !_sexView.maleBtn.selected;
    _sexView.femaleBtn.selected = !_sexView.maleBtn.selected;
    
}
- (void)femaleAct:(UIButton *)btn{
    if (btn.isSelected) {
        return;
    }
    self.sex = @"2";
    _sexView.femaleBtn.selected = !_sexView.femaleBtn.selected;
    _sexView.maleBtn.selected = !_sexView.femaleBtn.selected;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
